package com.ciberiasoluciones.cibervoto.utility;

import java.util.Date;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener()
public class ListenerSesion implements HttpSessionListener {
    private int sessionCount=0;
    
    @Override
    public void sessionCreated(HttpSessionEvent se) {
         synchronized (this) {
            sessionCount++;
            
            if(sessionCount > 1)
            {
                //se.getSession().invalidate();
                System.out.println("Sesión Cerrada: " + se.getSession().getId());
            }
        } 
        System.out.println(" ---- Sesión creada: " + se.getSession().getId());
        System.out.println(" ---- Sesiones Totales: " + sessionCount);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        synchronized (this) {
            sessionCount--;
            
        HttpSession session = se.getSession();
        session.getAttribute(session.getId());
        
        System.out.println(" ---->>> " + getTime());
        System.out.println(" ---- Sesión destruida ID ="+ session.getAttribute(session.getId()));
        System.out.println(" ---- Sesiones totales: " + sessionCount);
        }
        
    }
    
    private String getTime()
    {
        return new Date(System.currentTimeMillis()).toString();
    }
}
